var XYZ;
(function (XYZ) {
    function OnLoad(load) {
        window.addEventListener("load", function (e) {
            load();
        });
    }
    XYZ.OnLoad = OnLoad;
    function OnWindowResize(resize) {
        window.addEventListener("resize", function (e) {
            resize(e);
        }, false);
    }
    XYZ.OnWindowResize = OnWindowResize;
    var XYZArray = (function () {
        function XYZArray() {
            this.all = {};
            this.id = 0;
        }
        XYZArray.prototype.add = function (obj) {
            this.id = this.id + 1;
            this.all[this.id] = obj;
        };
        XYZArray.prototype.each = function (ac) {
            for (var key in this.all) {
                var item = this.all[key];
                ac(key, item);
            }
        };
        XYZArray.prototype.remove = function (id) {
            if (this.all[id]) {
                delete this.all[id];
            }
        };
        return XYZArray;
    }());
    XYZ.XYZArray = XYZArray;
    var Play = (function () {
        function Play() {
        }
        Play.prototype.init = function (canvasId) {
            this.body = document.getElementsByTagName("body")[0];
            this.canvas = document.getElementById(canvasId);
            this.setCanvasWH();
        };
        Play.prototype.setCanvasWH = function () {
            this.canvas.setAttribute("width", window.innerWidth.toString());
            this.canvas.setAttribute("height", window.innerHeight.toString());
        };
        return Play;
    }());
    XYZ.Play = Play;
    var Point = (function () {
        function Point(x, y) {
            this.X = x;
            this.Y = y;
        }
        Point.random = function (maxX, maxY) {
            var x = Math.random() * maxX;
            var y = Math.random() * maxY;
            return new Point(x, y);
        };
        return Point;
    }());
    XYZ.Point = Point;
    var Circle = (function () {
        function Circle(ctx, x, y, radius) {
            this.X = x;
            this.Y = y;
            this.Radius = radius;
            this.Ctx = ctx;
            this.a = 1;
        }
        Circle.prototype.update = function () {
            if (this.Radius < 1) {
                this.Radius = 1;
            }
            else {
                this.Radius = this.Radius - 0.2;
            }
        };
        Circle.prototype.draw = function () {
            var color = "rgba(" + this.r + "," + this.g + "," + this.b + "," + this.a + ")";
            this.Ctx.fillStyle = color;
            this.Ctx.beginPath();
            this.Ctx.moveTo(this.X, this.Y);
            this.Ctx.arc(this.X, this.Y, this.Radius, 0, Math.PI * 2, true);
            this.Ctx.fill();
        };
        return Circle;
    }());
    XYZ.Circle = Circle;
    var Particle = (function () {
        function Particle(place, life, dc) {
            this.lifetime = life;
            this.direction = dc;
            this.place = place;
            this.age = 0;
            this.die = false;
        }
        Particle.prototype.update = function () {
            this.age = this.age + 1;
            this.place.X = this.place.X + this.direction.X;
            this.place.Y = this.place.Y + this.direction.Y;
            this.graph.update();
            if (this.graph.Radius <= 1) {
                this.die = true;
            }
            this.graph.a = (this.lifetime - this.age) / this.lifetime;
            if (this.age > this.lifetime) {
                this.die = true;
            }
        };
        Particle.prototype.draw = function () {
            if (this.graph) {
                this.graph.X = this.place.X;
                this.graph.Y = this.place.Y;
                this.graph.draw();
            }
        };
        return Particle;
    }());
    XYZ.Particle = Particle;
    var Game = (function () {
        function Game(id) {
            this.Canvas = document.getElementById(id);
            this.CTX = this.Canvas.getContext('2d');
        }
        Game.prototype.init = function () {
            this.cs = new XYZArray();
            this.times = 0;
        };
        Game.prototype.update = function () {
            var w = this.Canvas.width;
            var h = this.Canvas.height;
            var thisObj = this;
            var ysu = 0;
            this.cs.each(function (id, item) {
                ysu = ysu + 1;
                if (item.die) {
                    thisObj.cs.remove(id);
                }
                else {
                    item.update();
                }
            });
            //console.log(ysu);
            var tpMax = 0;
            for (var i = 0; i < (this.AudioArray.length); i++) {
                var value = this.AudioArray[i];
                if (value > 0) {
                    var g1 = this.CTX.createLinearGradient(0, 0, 0, h);
                    g1.addColorStop(1, 'rgb(0,255,0)'); //绿
                    g1.addColorStop(0.7, 'yellow'); //
                    g1.addColorStop(0, 'rgb(255,0,0)'); //红  
                    this.CTX.fillStyle = g1;
                    this.CTX.fillRect(i * 5, h, 3, h - value);
                }
                if (value > tpMax) {
                    tpMax = value;
                }
            }
            if (tpMax > 0) {
                var x = Math.random() * w;
                var y = Math.random() * h;
                var c = new Circle(this.CTX, x, y, Math.random() * (tpMax / 4));
                c.r = Math.floor(Math.random() * 255);
                c.g = Math.floor(Math.random() * 255);
                c.b = Math.floor(Math.random() * 255);
                var p = new Particle(new Point(c.X, c.Y), tpMax, new Point(Math.random() * 4 - 2, Math.random() * 4 - 2));
                p.graph = c;
                this.cs.add(p);
            }
        };
        Game.prototype.render = function () {
            var w = this.Canvas.width;
            var h = this.Canvas.height;
            //console.log("w:"+w+",H:"+h);
            this.CTX.clearRect(0, 0, w, h);
            this.cs.each(function (id, item) {
                item.draw();
            });
        };
        Game.prototype.run = function () {
            this.update();
            this.render();
        };
        return Game;
    }());
    XYZ.Game = Game;
})(XYZ || (XYZ = {}));
